<template>
  <div class="dialog-form">
    <el-form ref="ticketpriceRef" :model="form" :rules="rules" label-width="80px">
      <el-row :gutter="36">
        <el-col :span="12">
          <el-form-item label="票价(元)" prop="priceDecimal">
            <el-input-number v-model="form.priceDecimal" :min="0.01" :precision="2" placeholder="仅支持输入纯数字，最多2位小数" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-switch
                v-model="form.status"
                inline-prompt
                :active-value="1"
                :inactive-value="2"
                inactive-text="禁用"
                active-text="启用"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="票价描述" prop="description">
            <el-input v-model="form.description" type="textarea" :maxlength="100" show-word-limit placeholder="请输入内容，最多100个字" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onConfirm">确认</el-button>
    </div>
  </div>
</template>

<script setup>
import { getTicketprice, addTicketprice, updateTicketprice } from "@/api/bus/ticketprice"

const { proxy } = getCurrentInstance()

const emits = defineEmits(['cancel', 'confirm'])

const ticketpriceRef = ref()
const form = ref({
  id: null,
  price: null,
  priceDecimal: null,
  description: null,
  status: 1,
  createTime: null,
  updateTime: null
})
const rules = {
  priceDecimal: [
    { required: true, message: "票价不能为空", trigger: "blur" }
  ],
  status: [
    { required: false, message: "状态不能为空", trigger: "change" }
  ]
}

const pageInit = (id) => {
  if (id) {
    getTicketprice(id).then(response => {
      form.value = response.data
    })
  }
}

const onCancel = () => {
  emits('cancel')
}

const onConfirm = () => {
  ticketpriceRef.value.validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTicketprice(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          ticketpriceRef.value.resetFields()
          emits('confirm')
        })
      } else {
        addTicketprice(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          ticketpriceRef.value.resetFields()
          emits('confirm')
        })
      }
    }
  })
}

defineExpose({
  pageInit
})

</script>

<style lang="scss" scoped>

</style>